<?
namespace xto\data\service;

use xto\data\model\PointModel;
use xto\membership\core\IUser;
use xto\membership\core\UserRole;
use xto\Util;
use xto\App;

class PointService{
	static function create(IUser $user,$income=0,$expenses=0,$remark){
		if($user->userrole!=UserRole::Member){
			return false;
		}
		$data=[
			'userid' 	=> $user->userid,
			'username'	=> $user->username,
			'tradedate'	=> Util::getdate(),
			'income' 	=> $income, 
			'expenses' 	=> $expenses,
			'appid' 	=> App::appid(),
			'remark' 	=> $remark,
			'balance' 	=> $user->points+$income-$expenses
			]; 
		return PointModel::create($data);
	}

	static function getlist($pagesize){
		$result=array();
		$list = PointModel::order('pid', 'desc')->paginate($pagesize);
		$page = $list->render();
		$result['list']=$list;
		$result['page']=$page;
		return $result;
	}
}